// miniprogram/pages/word/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    word: {
      word: "少女祈祷中",
      pronunciation: "",
      cixing: "",
      level: "",
      meaning: "",
      star: false
    },
    star: false,
    isLoad: true,
  },

  /**
   * 播放音频
   */
  play() {
    const thiz = this
    let audio = wx.createInnerAudioContext()
    console.log(audio)
    audio.src = encodeURI("http://dict.youdao.com/dictvoice?audio=" + this.data.word.word + "&le=jap")
    audio.play()
    this.setData({
      playing: true
    })
    audio.onEnded(function() {
      console.log("onEnd")
      thiz.setData({
        playing: false
      })
    })
  },

  /**
   * 点击收藏按钮
   */
  collect: function(e) {
    const thiz = this
    if (!this.data.star) { //开始收藏
      thiz.setData({
        star: true
      })
      wx.cloud.callFunction({
        name: 'star',
        data: {
          word_id: thiz.data.word.word_id,
        },
        success: function(res) {
          console.log("call addCollect success:", res)
          if (res.result.msg == 'ok') {
            console.log("收藏成功")
          } else {
            wx.showToast({
              icon: 'none',
              title: '收藏失败',
            })
          }
        },
        fail: function(res) {
          console.log("call addCollect fail:", res)
        }
      })
    } else { //取消收藏
      thiz.setData({
        star: false
      })
      wx.cloud.callFunction({
        name: 'unstar',
        data: {
          word_id: thiz.data.word.word_id,
        },
        success: function(res) {
          console.log(res)
          if (res.result.msg == 'ok') {
            console.log("取消收藏成功")
          } else {
            wx.showToast({
              icon: 'none',
              title: '取消收藏失败',
            })
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    }

    //动画
    console.log(e)
    thiz.setData({
      animation: 'shake'
    })
    setTimeout(function() {
      thiz.setData({
        animation: '',
      })
    }, 1000)

  },

  shakes(e) {
    const thiz = this
    //动画
    console.log(e)
    thiz.setData({
      animation: 'shake'
    })
    setTimeout(function() {
      thiz.setData({
        animation: ''
      })
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var thiz = this
    const wordId = Number(options.wordId)
    console.log("wordid", wordId)
    //根据wordId获取数据库中的单词

    wx.cloud.callFunction({
      name: 'word',
      data: {
        word_id: wordId
      },
      success: function(res) {
        console.log("call word success:", res)
        thiz.setData({
          word: res.result,
          star: res.result.star,
          isLoad: false
        })
      },
      fail: function(res) {
        console.log("call word fail:", res)
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})